package com.app.controller;

import com.app.common.Result;
import com.app.model.BusinessModel;
import com.app.model.UserModel;
import com.app.model.VerificationRecordModel;
import com.app.service.BusinessService;
import com.app.service.UserService;
import com.app.service.VerificationRecordService;
import com.app.utils.TokenUtils;
import com.app.vo.VerificationRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 商家核销记录表 前端控制器
 * </p>
 *
 * @author 16693103420
 * @since 2024-01-31
 */
@RestController
@RequestMapping("/verificationRecord")
public class VerificationRecordController {
    @Autowired
    private VerificationRecordService verificationRecordService;
    @Autowired
    private UserService userService;
    @Autowired
    private BusinessService businessService;

    // 查询列表
    @GetMapping("/getVerificationRecordList")
    public Result getVerificationRecordList(@RequestParam(defaultValue = "0") Integer page,
                                            @RequestParam(defaultValue = "10") Integer limit, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        BusinessModel businessModel = businessService.getByUserPhone(userModel.getPhone());
        if (businessModel == null) {
            return Result.success("您好,您不是商家身份");
        }
        VerificationRecordVo verificationRecordVo = verificationRecordService.getVerificationRecordList(page, limit, businessModel.getId());
        return Result.success(verificationRecordVo);
    }

    // 商家核销
    @PostMapping("/businessVerification")
    public Result businessVerification(@RequestBody VerificationRecordModel verificationRecordModel, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用户不存在");
        }
        if (userModel.getStatus() != 2) {
            return Result.error(201, "请商家进行核销");
        }
        String orderId = verificationRecordModel.getOrderId();
        String message = verificationRecordService.businessVerification(orderId, userModel);
        if ("核销成功".equals(message)) {
            return Result.success(message);
        } else {
            return Result.error(201, message);
        }
    }

    //根据id查询
    @GetMapping("/getVerificationRecordById")
    public Result getVerificationRecordById(Integer id) {
        VerificationRecordModel verificationRecord = verificationRecordService.getById(id);
        return Result.success(verificationRecord);
    }

    //根据id删除
    @GetMapping("/delVerificationRecord")
    public Result delVerificationRecord(Integer id) {
        verificationRecordService.removeById(id);
        return Result.success("删除成功");
    }
}

